﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.Common;
using Actuals.Business;

namespace Actuals
{
    public partial class AddRelease : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Mode"] != null)
                {
                    string _mode = Request.QueryString["Mode"].ToString();

                    IBusinessTeam businessTeam = new BusinessTeam();
                    IList<ITeam> teams = businessTeam.GetTeams();

                    ddlTeam.DataSource = teams;
                    ddlTeam.DataValueField = "TeamId";
                    ddlTeam.DataTextField = "TeamDescription";
                    ddlTeam.DataBind();
                    ddlTeam.Items.Insert(0, "Select");


                    string _projectId = Request.QueryString["ProjectId"].ToString();
                        hidProjectId.Value = _projectId;
                     if (_mode == "Edit")                   
                    {
                        
                        string _releaseId = Request.QueryString["ReleaseId"].ToString();
                        ViewState["ReleaseId"] = _releaseId;
                        PopulateRelease(int.Parse(_projectId), int.Parse(_releaseId));
                        
                    }
                }
            }

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                IRelease release = new Release();
                release.Description = txtReleaseDescription.Text;
                string[] _date1 = txtStartDate.Text.Split('-');
                release.StartDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));
                string[] _date2 = txtEndDate.Text.Split('-');
                release.EndDate = new DateTime(int.Parse(_date2[2]), int.Parse(_date2[0]), int.Parse(_date2[1]));
                release.ProjectId =int.Parse(hidProjectId.Value);
                release.TeamId =  int.Parse(ddlTeam.SelectedItem.Value);
                IBusinessRelease businessRelease = new BusinessRelease();
                if (Request.QueryString["Mode"] == "Add")
                {
                    businessRelease.AddRelease(release);
                    lblMsg.Text = "Added";
                }
                else
                {
                    release.ReleaseId = int.Parse(ViewState["ReleaseId"].ToString());
                    businessRelease.UpdateRelease(release);
                }
                 
                lblMsg.Text = "Updated";

            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;

            }
        }

        protected void PopulateRelease(int projectId,int releaseId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IRelease release = businessRelease.GetRelease(projectId,releaseId);
            txtReleaseDescription.Text = release.Description;
            txtStartDate.Text = release.StartDate.ToString("MM-dd-yyyy");
            txtEndDate.Text = release.EndDate.ToString("MM-dd-yyyy");
            if (release.TeamId != null)
                ddlTeam.SelectedValue = release.TeamId.ToString();

        }
       
    }
}